package com.yykj.tencent.im.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.yykj.common.util.JsonUtil;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.tencent.im.client.form.QueryMsgDto;
import com.yykj.tencent.im.client.form.QueryMsgForm;
import com.yykj.tencent.im.server.common.ResponseVo;
import com.yykj.tencent.im.server.dto.C2CMessageDTO;
import com.yykj.tencent.im.server.form.C2CPageForm;
import com.yykj.tencent.im.server.service.C2CMessageService;
import com.yykj.tencent.im.server.service.FriendService;
import com.yykj.tencent.im.server.service.GroupMessageService;
import com.yykj.tencent.im.server.service.ImService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "腾讯云通信")
@RequestMapping("api/tencent/im")
public class TencentImController {
    @Value("${environment}")
    private String environment;
    @Resource
    private C2CMessageService c2CMessageService;
    @Resource
    private GroupMessageService groupMessageService;

    @Resource
    private FriendService friendService;
    @Resource
    private ImService imService;



    @ApiOperation("接收回调")
    @PostMapping("/callback")
    public ResponseVo callback(@RequestBody String str){
        JSONObject jsonObject = JSONObject.parseObject(str);
        String command = jsonObject.getString("CallbackCommand");
        switch (command){
            case "Sns.CallbackFriendAdd":
                Map map = JsonUtil.fromJson(str, Map.class);
                friendService.addFriendCallback(map);
                break;
            case "Sns.CallbackFriendDelete":
                Map mapz = JsonUtil.fromJson(str, Map.class);
                friendService.deleteFriendCallback(mapz);
                break;
            case "C2C.CallbackAfterSendMsg":
                this.imService.addMessage(str, 0);
//                c2CMessageService.addC2CMessage(map);
                break;
            case "Group.CallbackAfterSendMsg":
                this.imService.addMessage(str, 1);
//                groupMessageService.addGroupMessage(map);
                break;
        }
        log.info("线程");
        return ResponseVo.success();
    }

    @ApiOperation("获取单聊消息")
    @GetMapping("/c2c/message")
    public ResultVO<PageData<C2CMessageDTO>> c2cMessage(
            @RequestParam(name = "fromAccount", required = true) String fromAccount,
            @RequestParam(name = "toAccount", required = true) String toAccount,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
            ){
        C2CPageForm form = C2CPageForm.builder()
                .fromAccount(fromAccount)
                .toAccount(toAccount)
                .keyword(keyword)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        return ResultVO.success(c2CMessageService.listC2CMessages(form));
    }

    @ApiOperation("获取UserSig")
    @GetMapping("/usersig/get")
    public String getUserSig(){
         return imService.getUserSig();
    }

    @ApiOperation("搜索聊天消息")
    @PostMapping("/queryMsg")
    public ResultVO<PageData<QueryMsgDto>> queryMsg(@RequestBody QueryMsgForm queryMsgForm){
        /*if(!"prod".equals(environment)){//非生产环境都转发到生产
            String s = HttpUtil.jsonSend("https://gateway.yaoyanglao.com//tencent-im-server/api/tencent/im/queryMsg", null, null, JsonUtil.toJson(queryMsgForm));
            if(StringUtils.isNotBlank(s)){
                return JSON.parseObject(s, ResultVO.class);
            }
            return null;
        }*/
        return ResultVO.success(imService.queryMsg(queryMsgForm));
    }

}
